package com.chen.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Constants;
import com.chen.common.Result;
import com.chen.entity.Details;
import com.chen.entity.User;
import com.chen.entity.vo.UserDTO;
import com.chen.entity.vo.UserPasswordDTO;
import com.chen.mapper.DetailsMapper;
import com.chen.mapper.RequestMapper;
import com.chen.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y·C
 * @since 2023-04-19
 */
@Api(tags = "登录用户信息接口")
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;

    @Resource
    private DetailsMapper detailsMapper;

    @Resource
    private RequestMapper requestMapper;

    @ApiOperation("管理员登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {


        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        UserDTO dto = userService.login(userDTO);
        return Result.success(dto);
    }

    @ApiOperation("管理员注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO) {


        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        return Result.success(userService.register(userDTO));
    }

    /**
     * 修改密码
     * @param userPasswordDTO
     * @return
     */
    @ApiOperation("管理员修改密码")
    @PostMapping("/password")
    public Result password(@RequestBody UserPasswordDTO userPasswordDTO) {

        userPasswordDTO.setPassword(SecureUtil.md5(userPasswordDTO.getPassword()));
        userPasswordDTO.setNewPassword(SecureUtil.md5(userPasswordDTO.getNewPassword()));
        userService.updatePassword(userPasswordDTO);
        return Result.success();
    }


    @ApiOperation("展示用户基础信息")
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "")  String username){

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username",username);
        return Result.success(userService.page(new Page<>(pageNum, pageSize), wrapper));

    }

    @ApiOperation("展示用户详细信息")
    @PostMapping("/details/{id}")
    public Result details(@PathVariable Integer id){

        return  Result.success(detailsMapper.selectById(id));
    }



    @ApiOperation("展示择偶要求信息")
    @PostMapping("/request/{id}")
    public Result request(@PathVariable Integer id){

        return  Result.success(requestMapper.selectById(id));
    }

    @ApiOperation("删除用户基本信息")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(userService.removeByIds(ids));
    }


    @ApiOperation("批量审核通过")
    @PostMapping("/audit")
    public Result audit(@RequestBody List<Integer> ids){
        userService.adoptByCids(ids);
        return Result.success();
    }

    @ApiOperation("锁定用户或取消锁定")
    @PostMapping("lock/{id}/{state}")
    public Result lock( @PathVariable Integer id, @PathVariable Integer state) {

        userService.lock(id,state);

        return Result.success();
    }

}

